package com.zyyoona7.wheelpicker.slice;

import com.zyyoona7.wheel.WheelView;
import com.zyyoona7.wheelpicker.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.NestedScrollView;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义属性展示
 *
 * @author rpf
 * @since 2021-04-09
 */
public class Main2AbilitySlice extends AbilitySlice {
    private static final int TWENTY = 20;
    private static final int FIVE = 5;
    private static final int NINE = 9;
    private static final String JING = "#";
    private HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x001, "TAG");
    private WheelView<String> wheelView;
    private NestedScrollView nestedScrollView;
    private Text currentItemDataTv;
    private Text currentItemPositionTv;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main2);

        initComponent();

        initShow();
        initShow2();
        initShow3();

        initListener();
    }

    /**
     * 初始化
     */
    private void initComponent() {
        nestedScrollView = (NestedScrollView) findComponentById(ResourceTable.Id_nestedScrollView);
        wheelView = (WheelView<String>) findComponentById(ResourceTable.Id_wheelView);
        List<String> list = new ArrayList<>(1);
        for (int i1 = 0; i1 < TWENTY; i1++) {
            if (i1 <= NINE) {
                list.add("00" + i1);
            } else {
                list.add("0" + i1);
            }
        }
        wheelView.setData(list);

        nestedScrollView.doFlingY(FIVE);

        Button showDataBtn = (Button) findComponentById(ResourceTable.Id_btn_showSelectedData);

        showDataBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(logLabel, "-----" + wheelView.getSelectedItemData());
                new ToastDialog(getContext()).setText("选中的值为：" + wheelView.getSelectedItemData()).show();
            }
        });

        /**
         * 3D效果暂无
         */

        wheelView.setOnWheelChangedListener(new WheelView.OnWheelChangedListener() {
            @Override
            public void onWheelScroll(int scrollOffsetY) {
                HiLog.error(logLabel, "scrollOffsetY-" + scrollOffsetY);
            }

            @Override
            public void onWheelItemChanged(int oldPosition, int newPosition) {
                wheelView.setSelectedItemPosition(newPosition);
                HiLog.error(logLabel, "oldPosition-" + oldPosition + "newPosition-" + newPosition);
            }

            @Override
            public void onWheelSelected(int position) {
            }

            @Override
            public void onWheelScrollStateChanged(int state) {
                showDataBtn.setEnabled(state == WheelView.SCROLL_STATE_IDLE);
            }
        });

        wheelView.setNestedScrollView(nestedScrollView);
    }

    private void initShow() {
        Text textSizeTv = (Text) findComponentById(ResourceTable.Id_tv_text_size);
        textSizeTv.setText(getString(ResourceTable.String_textSizeValue, String.valueOf(wheelView.getTextSize())));
        Text textAlignTv = (Text) findComponentById(ResourceTable.Id_tv_text_align);
        String textAlign = "Center";
        if (wheelView.getTextAlign() == WheelView.TEXT_ALIGN_LEFT) {
            textAlign = "Left";
        } else if (wheelView.getTextAlign() == WheelView.TEXT_ALIGN_RIGHT) {
            textAlign = "right";
        }
        textAlignTv.setText(getString(ResourceTable.String_textAlignValue, textAlign));

        Text textBoundaryMarginTv = (Text) findComponentById(ResourceTable.Id_tv_textBoundaryMargin);
        textBoundaryMarginTv.setText(getString(ResourceTable.String_textBoundaryMarginValue,
                String.valueOf(wheelView.getTextBoundaryMargin())));
        Text normalItemTextColorTv = (Text) findComponentById(ResourceTable.Id_tv_normalItemTextColor);
        normalItemTextColorTv.setText(getString(ResourceTable.String_normalItemTextColorValue,
                JING + Math.abs(wheelView.getNormalItemTextColor().getValue())));
        Text selectedItemTextColorTv = (Text) findComponentById(ResourceTable.Id_tv_selectedItemTextColor);
        selectedItemTextColorTv.setText(getString(ResourceTable.String_selectedItemTextColorValue,
                JING + Math.abs(wheelView.getSelectedItemTextColor().getValue())));
        Text lineSpaceTv = (Text) findComponentById(ResourceTable.Id_tv_lineSpace);
        lineSpaceTv.setText(getString(ResourceTable.String_lineSpaceValue, String.valueOf(wheelView.getLineSpacing())));
        Text integerNeedFormatTv = (Text) findComponentById(ResourceTable.Id_tv_integerNeedFormat);
        integerNeedFormatTv.setText(getString(ResourceTable.String_integerNeedFormatValue,
                String.valueOf(wheelView.isIntegerNeedFormat())));
        Text integerFormatTv = (Text) findComponentById(ResourceTable.Id_tv_integerFormat);
        integerFormatTv.setText(getString(ResourceTable.String_integerFormatValue, wheelView.getIntegerFormat()));
        Text visibleItemsTv = (Text) findComponentById(ResourceTable.Id_tv_visibleItems);
        visibleItemsTv.setText(getString(ResourceTable.String_visibleItemsValue,
                String.valueOf(wheelView.getVisibleItems())));
    }

    private void initShow2() {
        currentItemPositionTv = (Text) findComponentById(ResourceTable.Id_tv_currentItemPosition);
        currentItemPositionTv.setText(getString(ResourceTable.String_currentItemPositionValue,
                String.valueOf(wheelView.getSelectedItemPosition())));
        currentItemDataTv = (Text) findComponentById(ResourceTable.Id_tv_currentItemData);
        currentItemDataTv.setText(getString(ResourceTable.String_currentItemDataValue,
                String.valueOf(wheelView.getSelectedItemPosition())));
        Text showDividerTv = (Text) findComponentById(ResourceTable.Id_tv_showDivider);
        showDividerTv.setText(getString(ResourceTable.String_showDividerValue,
                String.valueOf(wheelView.isShowDivider())));
        Text dividerTypeTv = (Text) findComponentById(ResourceTable.Id_tv_dividerType);
        String dividerType = "fill";
        if (wheelView.getDividerType() == WheelView.DIVIDER_TYPE_WRAP) {
            dividerType = "wrap";
        }
        dividerTypeTv.setText(getString(ResourceTable.String_dividerTypeValue, dividerType));
        Text dividerColorTv = (Text) findComponentById(ResourceTable.Id_tv_dividerColor);
        dividerColorTv.setText(getString(ResourceTable.String_dividerColorValue,
                JING + Math.abs(wheelView.getDividerColor().getValue())));
        Text dividerHeightTv = (Text) findComponentById(ResourceTable.Id_tv_dividerHeight);
        dividerHeightTv.setText(getString(ResourceTable.String_dividerHeightValue,
                String.valueOf(wheelView.getDividerHeight())));
        Text dividerPaddingForWrapTv = (Text) findComponentById(ResourceTable.Id_tv_dividerPaddingForWrap);
        dividerPaddingForWrapTv.setText(getString(ResourceTable.String_dividerPaddingForWrapValue,
                String.valueOf(wheelView.getDividerPaddingForWrap())));

        Text curvedRefractXtv = (Text) findComponentById(ResourceTable.Id_tv_curvedRefractRatio);
        curvedRefractXtv.setText(getString(ResourceTable.String_curvedRefractXValue,
                wheelView.getRefractRatio()));
    }

    private void initShow3() {
        Text curvedTv = (Text) findComponentById(ResourceTable.Id_tv_curved);
        curvedTv.setText(getString(ResourceTable.String_curvedValue,
                false));
        Text curvedArcDirectionTv = (Text) findComponentById(ResourceTable.Id_tv_curvedAlign);
        String curvedArcDirection = "Center";
        curvedArcDirectionTv.setText(getString(ResourceTable.String_curvedArcDirectionValue, curvedArcDirection));
        Text curvedArcDirectionBiasTv = (Text) findComponentById(ResourceTable.Id_tv_curvedAlignBias);
        curvedArcDirectionBiasTv.setText(getString(ResourceTable.String_curvedArcDirectionBiasValue,
                curvedArcDirection));
    }

    private void initListener() {
        wheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(WheelView<String> wheelView1, String data, int position) {
                currentItemPositionTv.setText(getString(ResourceTable.String_currentItemPositionValue,
                        String.valueOf(position)));
                currentItemDataTv.setText(getString(ResourceTable.String_currentItemDataValue,
                        String.valueOf(wheelView.getSelectedItemData())));
            }
        });
    }
}
